/**
 * @file        configuration.ipp
 * @brief       Configuration class implementation.
 *
 * @author      Emre Turkay <emreturkay at gmail>
 * @date        Jan.05.2009
 *
 * Copyright Emre Turkay 2009
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file COPYRIGHT or copy at
 * http://www.boost.org/LICENSE_1_0.txt)
 */

#include <boost/date_time.hpp>

#include <sys/stat.h>

#include <cstdlib>
#include <stdexcept>

namespace gtd
{

inline
configuration::configuration()
{
#ifndef WIN32
    const char* home = getenv("HOME");
#else
    const char* home = getenv("APPDATA");
#endif

    if (!home)
        throw std::runtime_error("HOME is not defined");

    path_ = std::string(home) + "/gtd/";

    todo_file_ = path_ + "todo.txt";
    done_file_ = path_ + "done.txt";
    tmp_file_ = path_ + ".todo.txt";

    struct stat tmp;
    if (stat(path_.c_str(), &tmp))
        mkdir(path_.c_str(), 0700);

    if (stat(todo_file_.c_str(), &tmp))
        std::ofstream f(todo_file_.c_str());

    if (stat(done_file_.c_str(), &tmp))
        std::ofstream f(done_file_.c_str());
}

inline
const std::string& configuration::todo_file() const
{
    return todo_file_;
}

inline
const std::string& configuration::done_file() const
{
    return done_file_;
}

inline
const std::string& configuration::path() const
{
    return path_;
}

inline
const std::string& configuration::tmp_file() const
{
    return tmp_file_;
}

inline
std::string configuration::backup_file() const
{
    std::ostringstream os;
    boost::posix_time::time_facet* facet(
        new boost::posix_time::time_facet("todo.%Y_%m_%d_%H_%M_%S.backup"));
    os.imbue(std::locale(os.getloc(), facet));
    os << boost::posix_time::second_clock::local_time();
    return path_ + "backup/" + os.str();
}

} // namespace gtd

